﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Test_WebDriver
{
	/// <summary>
	///		<para>Summary: Basic introduction demo for Selenium Driver on IE.</para>
	///		<para>Author: Heller (Hellersong@outlook.com)</para>
	///		<para>Creation: 7/11/2015 10:22:41 AM</para>
	/// </summary>
    [TestClass]
    public class TestAbcIE
    {
        /**
         * 1) The security of IE browser must be set to the same value
         * 2) Give the right IEDriverServer.exe
		 * 3) Page Zoom in 100%
         */
		private static String driverServerPath = "";
		private IWebDriver ieDriver = null;
		[TestInitialize]
		public void Setup()
		{
			String s1 = Directory.GetParent(Environment.CurrentDirectory).ToString();
			String s2 = Directory.GetParent(s1).ToString();
			driverServerPath = s2 + @"\DriverServer\IEDriverServer-64";
		}

		[TestMethod]
		public void test_open_ie_with_spcified_url()
		{
			ieDriver = new InternetExplorerDriver(driverServerPath);
			ieDriver.Navigate().GoToUrl(@"http://www.baidu.com");
		}

		[TestMethod]
		public void test_find_element_and_take_action()
		{
			// Initialize
			ieDriver = new InternetExplorerDriver(driverServerPath);
			ieDriver.Manage().Window.Maximize();

			Thread.Sleep(1000);
			ieDriver.Navigate().GoToUrl(@"http://www.baidu.com");
			Thread.Sleep(3000);

            Actions myAction = new Actions(ieDriver);
			Console.WriteLine(ieDriver.Title);
			IWebElement txtBox = ieDriver.FindElement(By.Id("kw"));
			//IWebElement txtBox = driver.FindElement(By.Name("wd"));
            Thread.Sleep(2000);
			txtBox.SendKeys("Ss");

			IWebElement btnSearch = ieDriver.FindElement(By.Id("su"));
			myAction.MoveToElement(btnSearch);
			myAction.ContextClick(btnSearch);	// Right click
			myAction.Build().Perform();
			Thread.Sleep(2000);
			myAction.SendKeys(Keys.Escape);	// Type keyboard "ESC"
			Thread.Sleep(2000);
			
			btnSearch.Click();

            
			//ieDriver.Manage().Timeouts().ImplicitlyWait(System.TimeSpan.FromSeconds(10));
		}

		[TestMethod]
		public void test_js_executor()
		{
			// Initialize
			ieDriver = new InternetExplorerDriver(driverServerPath);
			ieDriver.Manage().Window.Maximize();

			Thread.Sleep(1000);
			ieDriver.Navigate().GoToUrl(@"http://www.baidu.com");
			Thread.Sleep(3000);

			IWebElement txtBox = ieDriver.FindElement(By.Id("kw"));
			IJavaScriptExecutor js = ieDriver as IJavaScriptExecutor;
			js.ExecuteScript("arguments[0].value='Ss';", txtBox);

			Thread.Sleep(2000);
			IWebElement btnSearch = ieDriver.FindElement(By.Id("su"));
			btnSearch.Click();
			Thread.Sleep(2000);
		}

		[TestMethod]
		public void test_all_ways_click_element()
		{
			// Initialize
			ieDriver = new InternetExplorerDriver(driverServerPath);
			ieDriver.Manage().Window.Maximize();

			Thread.Sleep(1000);
			ieDriver.Navigate().GoToUrl(@"http://www.baidu.com");
			Thread.Sleep(3000);

			IWebElement hao123Link = ieDriver.FindElement(By.CssSelector("a[href='http://www.hao123.com']"));

			//hao123Link.Click();

			//js.ExecuteScript("arguments[0].click();", btn);

			Actions myAction = new Actions(ieDriver);
			myAction.SendKeys(hao123Link, Keys.Return);
			myAction.Build().Perform();

			Thread.Sleep(2000);
		}

		[TestMethod]
		public void test_html_unit_driver()
		{
			//    HtmlUnitDriver driver = new HtmlUnitDriver(driverServerPath);
			//    WebDriver driver = new HtmlUnitDriver();
			//driver.get("http://www.google.com");
			//WebElement element = driver.findElement(By.name("q"));
			//element.sendKeys("Selenium");
			//element.submit();
			//System.out.println("Page title is: " + driver.getTitle());
			//// google查询结果是通过javascript动态呈现的. 
			//// 设置页面等待10秒超时
			//(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>()
			//{
			//    public Boolean apply(WebDriver d)
			//    {
			//        return d.getTitle().toLowerCase().startsWith("selenium");
			//    }
			//});
			//System.out.println("Page title is: " + driver.getTitle());
		}

		[TestCleanup]
		public void CleanUp()
		{
			Thread.Sleep(3000);
			ieDriver.Quit();
			ieDriver.Dispose();
		}
    }
}
